package soar.template.springboot.mq.alimq.producer.configuration.alimq;

import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;

import soar.template.springboot.mq.alimq.producer.appconfig.AppConfig;
import soar.template.springboot.mq.alimq.producer.appconfig.alimq.Config;

@Configuration
public class MyAliMQConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    /*--- 发送 ---*/
    @Bean(name = "sendMainProducer", initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean sendMainProducer()
    {
       return create_producer(appconfig.alimq.sendMain);
    }
    
    private ProducerBean create_producer(Config config)
    {
        ProducerBean producer = new ProducerBean();
        
        Properties props = new Properties();
        props.setProperty(PropertyKeyConst.ProducerId, config.producerId);
        props.setProperty(PropertyKeyConst.AccessKey, config.accessKey);
        props.setProperty(PropertyKeyConst.SecretKey, config.secretKey);
        props.setProperty(PropertyKeyConst.ONSAddr, config.ONSAddr);
        producer.setProperties(props);
        
        return producer;
    }
}
